<?php

namespace Medicina\BackendBundle\Form;

use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Mailer\MailerInterface;

class RegistrationFormHandler {

//    protected $request;
//    protected $userManager;
//    protected $form;
//    
//
//    public function __construct(Form $form, Request $request, UserManagerInterface $userManager) {
//        $this->form = $form;
//        $this->request = $request;
//        $this->userManager = $userManager;
//       
//    }

    protected $request;
    protected $userManager;
    protected $form;
    protected $mailer;

    protected $usuarioActual;
    
    public function __construct(Form $form, Request $request, UserManagerInterface $userManager, MailerInterface $mailer) {
        $this->form = $form;
        $this->request = $request;
        $this->userManager = $userManager;
        $this->mailer = $mailer;
    }

//    public function process($confirmation = null) {
////        $user = $this->userManager->createUser();
////        $this->form->setData($user);
////
////        if ('POST' == $this->request->getMethod()) {
////            $this->form->bindRequest($this->request);
////            
////            if ($this->form->isValid()) {
////                $this->userManager->createUser($user);
////                return true;
////            }
////
////            // Reloads the user to reset its username. This is needed when the
////            // username or password have been changed to avoid issues with the
////            // security layer.
////            $this->userManager->reloadUser($user);
////        }
////        return false;
//
//        $user = $this->userManager->createUser();
//        $this->form->setData($user);
//
//        if ('POST' == $this->request->getMethod()) {
//            $this->form->bindRequest($this->request);
//
//            if ($this->form->isValid()) {
//                if (true === $confirmation) {
//                    $user->setEnabled(false);
//                } else if (false === $confirmation) {
//                    $user->setConfirmationToken(null);
//                    $user->setEnabled(true);
//                }
////                $prog = new \Gymloop\CoreBundle\Entity\Programme();
////
////                $prog->setStartDate(date_create());
////                $prog->setEndDate(date_create());
////                $prog->setWeeklyTarget(4);
////                $prog->setGoal('');
////
////                $user->addProgrammes($prog);
//                $this->userManager->updateUser($user);
//
//                return true;
//            }
//        }
//
//        return false;
//    }
    ////////////////////////////////////7FOS USER/////////////////////
    ////////////////////////////////////7FOS USER/////////////////////
    ////////////////////////////////////7FOS USER/////////////////////
    ////////////////////////////////////7FOS USER/////////////////////
    public function process($confirmation = false) {     
        $user = $this->userManager->createUser();
        $this->form->setData($user);


        if ('POST' === $this->request->getMethod()) {
            $this->form->bindRequest($this->request);

            if ($this->form->isValid()) {
                
                //Para agregar los roles al usuario
                $formData = $this->request->get($this->form->getName());           
                $roles = $formData['roles'];
                foreach ($roles as $key => $value) {
                    $user->addRole($value);
                }
                //Para agregar los roles al usuario

                $this->onSuccess($user, $confirmation);

                return true;
            }
        }

        return false;
    }

    protected function onSuccess(UserInterface $user, $confirmation) {
        if ($confirmation) {
            $user->setEnabled(false);
            $this->mailer->sendConfirmationEmailMessage($user);
        } else {
            $user->setConfirmationToken(null);
            $user->setEnabled(true);
        }

        $this->userManager->updateUser($user);
        
        
        $user = $this->userManager->findUserByUsername('admin');
        
        $this->form->setData($user);
    }

}

